import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  deptList: [],
  currentDep: {},
  deptSeleteList: [],
  deptView: {},
  deptListPage: []
};

const getters = {
  deptView: state => state.deptView,
  deptList: state => state.deptList,
  currentDep: state => state.currentDep,
  deptSeleteList: state => state.deptSeleteList,
  deptListPage: state => state.deptListPage
};

const actions = {
  //新增部门
  deptSaveUsingPOSTS({ commit }, param) {
    return Api.deptSaveUsingPOST(param).then(ret => {
      return ret;
    });
  },
  //删除部门
  deptDelUsingDELETES({ commit }, param) {
    return Api.deptDelUsingPOST(param).then(ret => {
      return ret;
    });
  },
  //修改部门
  deptViewEditS({ commit }, param) {
    return Api.deptViewUsingGET(param).then(ret => {
      commit(types.DEPT_VIEW_EDIT, ret.data);
      return param;
    })

  },
  //分页列表
  deptListPageUsingPOSTS({ commit }, param) {
    return Api.deptListPageUsingGET(param).then(ret => {
      commit(types.LIST_DEPT_PAGE, ret.data);
      return ret;
    });
  },
  //部门查询 导出
  deptExcelUsingGETS({ commit }, param) {
    return Api.deptExcelUsingGET(param).then(ret => {
      return ret;
    });
  },
  //钉钉部门列表树状结构
  deptTreeUsingPOSTS({ commit }, param) {
    return Api.deptTreeUsingPOST(param).then(ret => {
      commit(types.UPDATE_DEPT_TREE, ret.data);
      return ret;
    });
  },
  //钉钉部门列表
  deptListUsingPOSTS({ commit }, param) {
    return Api.deptListUsingPOST_1(param).then(ret => {
      commit(types.UPDATE_DEPT_LIST, ret.data);
      return ret;
    });
  },
  //  钉钉部门详情
  deptViewUsingPOSTS({ commit }, param) {
    return Api.deptViewUsingPOST(param).then(ret => {
      commit(types.UPDATE_CURRENT_DEP, ret.data);
      return ret;
    });
  }
};

const mutations = {
  [types.DEPT_VIEW_EDIT](state, payload) {
    state.deptView = payload;
  },
  [types.UPDATE_DEPT_TREE](state, payload) {
    state.deptList = payload;
  },
  [types.UPDATE_DEPT_LIST](state, payload) {
    state.deptSeleteList = payload;
  },
  [types.UPDATE_CURRENT_DEP](state, payload) {
    state.currentDep = payload;
  },
  [types.LIST_DEPT_PAGE](state, payload) {
    state.deptListPage = payload;
  },
  [types.INIT_DEPT](state, payload) {
    state.deptView = {
      desc: "",
      dingId: "",
      id: "",
      level: 0,
      name: "",
      no: "",
      parentId: 0,
      phone: "",
      state: 0,
      deptType: '0',
      isCount: '0',
      type: ""
    };
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
